﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Stone
{
	/// <summary>
	/// Geographic position, Latitude and Longitude
	/// </summary>
	public class GPGLL
	{
        public string NMEA_string = ""; 

		/// <summary>
		/// Initializes the NMEA Geographic position, Latitude and Longitude
		/// </summary>
		public GPGLL()
		{
		}

		/// <summary>
		/// Initializes the NMEA Geographic position, Latitude and Longitude and parses an NMEA sentence
		/// </summary>
		/// <param name="NMEAsentence"></param>
		public GPGLL(string NMEAsentence)
		{
            NMEA_string = NMEAsentence;

			try
			{
				//Split into an array of strings.
				string[] split = NMEAsentence.Split(new Char[] { ',' });

				try
				{
					_position = new Coordinate(GlobalFunctions.GPSToDecimalDegrees(split[3], split[4]),
                                                GlobalFunctions.GPSToDecimalDegrees(split[1], split[2]));
				}
				catch { _position = null; }

				try
				{
					_timeOfSolution = new TimeSpan(int.Parse(split[5].Substring(0, 2)),
													int.Parse(split[5].Substring(2, 2)),
													int.Parse(split[5].Substring(4)));	
				}
				catch
				{
					_timeOfSolution = null; // TimeSpan.Zero;
				}
				_dataValid = (split[6] == "A");
			}
			catch { }
		}

		#region Properties

		private Coordinate _position ;
		private TimeSpan? _timeOfSolution;
		private bool _dataValid;

		/// <summary>
		/// Current position
		/// </summary>
		public Coordinate Position
		{
			get { return _position; }
			//set { _position = value; }
		}

		/// <summary>
		/// UTC Of Position Solution
		/// </summary>
		public TimeSpan? TimeOfSolution
		{
			get { return _timeOfSolution; }
			//set { _timeOfSolution = value; }
		}

		/// <summary>
		/// Data valid (true for valid or false for data invalid).
		/// </summary>
		public bool DataValid
		{
			get { return _dataValid; }
			//set { _dataValid = value; }
		}
	
		#endregion
	}
}
